package appTest;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;

public class CSVUtils{

    public File createCSVFile(List<List<String>> exportData, String outPutPath, String fileName) {
        File csvFile = null;
        BufferedWriter csvFileOutputStream = null;
        try {
            File file = new File(outPutPath);
            if (!file.exists()) {
                if (file.mkdirs()) {
                    System.out.println("创建成功");
                } else {
                    System.out.println("创建失败");
                }
            }
            //定义文件名格式并创建
            csvFile = File.createTempFile(fileName, ".csv", new File(outPutPath));
            csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), StandardCharsets.UTF_8), 1024);
            for (List<String> exportDatum : exportData) {
                writeRow(exportDatum, csvFileOutputStream);
                csvFileOutputStream.newLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (csvFileOutputStream != null) {
                    csvFileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return csvFile;
    }

    private void writeRow(List<String> row, BufferedWriter csvWriter) throws IOException {
        int i=0;
        for (String data : row) {
            csvWriter.write(DelQuota(data));
            if (i!=row.size()-1){
                csvWriter.write(",");
            }
            i++;
        }
    }

    public String DelQuota(String str) {
        String result = str;
        String[] strQuota = {"~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "`", ";", "'", ",", ".", "/", ":", "/,", "<", ">", "?"};
        for (int i = 0; i < strQuota.length; i++) {
            if (result.indexOf(strQuota[i]) > -1)
                result = result.replace(strQuota[i], "");
        }
        return result;
    }

    public void StrongFileTest(){
        CSVUtils csvUtils=new CSVUtils();
        List<List<String>> ll=new ArrayList<List<String>>();
        List<String> list=null;
        Triangle triangle = new Triangle();
        int i = 0;
        int n = 0;
        int flag = 0;
        while(true){
            list=new ArrayList<String>();
            if(i > 2)
                n = i + 96;
            else
                n = i;
            String a = Integer.toString(n);
            String b = "50";
            String c = "50";
            if(flag == 0){
                list.add(a);
                list.add(b);
                list.add(c);
                list.add(triangle.classify(Integer.parseInt(a),Integer.parseInt(b),Integer.parseInt(c)));
                ll.add(list);
                if(i == 5){
                    i = 0;
                    flag ++;
                    continue;
                }
            }
            else if(flag == 1){
                list.add(b);
                list.add(a);
                list.add(c);
                list.add(triangle.classify(Integer.parseInt(b),Integer.parseInt(a),Integer.parseInt(c)));
                ll.add(list);
                if(i == 5){
                    i = 0;
                    flag ++;
                    continue;
                }
            }
            else if(flag == 2){
                list.add(b);
                list.add(c);
                list.add(a);
                list.add(triangle.classify(Integer.parseInt(b),Integer.parseInt(c),Integer.parseInt(a)));
                ll.add(list);
                if(i == 5){
                    i = 0;
                    flag ++;
                    continue;
                }
            }
            else if(flag == 3){
                break;
            }
            i ++;
        }
        list=new ArrayList<String>();
        list.add("50");
        list.add("50");
        list.add("50");
        list.add(triangle.classify(Integer.parseInt("50"),Integer.parseInt("50"),Integer.parseInt("50")));
        ll.add(list);
        csvUtils.createCSVFile(ll,"D:\\app\\app\\src\\main\\resources","三角形健壮性边界测试用例");
    }
    public void WorstFileTest(){
        CSVUtils csvUtils=new CSVUtils();
        List<List<String>> ll=new ArrayList<List<String>>();
        List<String> list=null;
        Triangle triangle = new Triangle();
        int a = 1,b = 1,c = 1;
        for(int i = 1; i < 6; i ++) {
            b = 1;
            if(i == 3)
                a = 50;
            if(i == 4)
                a = 99;
            for(int j = 1; j < 6; j ++){
                c = 1;
                if(j == 3)
                    b = 50;
                if(j == 4)
                    b = 99;
                for(int k = 1; k < 6; k ++){
                    if(k == 3)
                        c = 50;
                    if(k == 4)
                        c = 99;
                    list=new ArrayList<String>();
                    list.add(Integer.toString(a));
                    list.add(Integer.toString(b));
                    list.add(Integer.toString(c));
                    list.add(triangle.classify(a,b,c));
                    ll.add(list);
                    c ++;
                }
                b ++;
            }
            a ++;
        }
        csvUtils.createCSVFile(ll,"D:\\app\\app\\src\\main\\resources","三角形最坏情况测试用例");
    }
    public void Strong_WorstFileTest(){
        CSVUtils csvUtils=new CSVUtils();
        List<List<String>> ll=new ArrayList<List<String>>();
        List<String> list=null;
        Triangle triangle = new Triangle();
        int a = 0,b = 0,c = 0;
        for(int i = 1; i < 8; i ++) {
            b = 0;
            if(i == 4)
                a = 50;
            if(i == 5)
                a = 99;
            for(int j = 1; j < 8; j ++){
                c = 0;
                if(j == 4)
                    b = 50;
                if(j == 5)
                    b = 99;
                for(int k = 1; k < 8; k ++){
                    if(k == 4)
                        c = 50;
                    if(k == 5)
                        c = 99;
                    list=new ArrayList<String>();
                    list.add(Integer.toString(a));
                    list.add(Integer.toString(b));
                    list.add(Integer.toString(c));
                    list.add(triangle.classify(a,b,c));
                    ll.add(list);
                    c ++;
                }
                b ++;
            }
            a ++;
        }
        csvUtils.createCSVFile(ll,"D:\\app\\app\\src\\main\\resources","三角形健壮性最坏情况测试用例");
    }
    public static void main(String[] args) {
        CSVUtils csvUtils = new CSVUtils();
        csvUtils.StrongFileTest();
        csvUtils.WorstFileTest();
        csvUtils.Strong_WorstFileTest();
    }

}
